package com.zjw.xm.xmorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.common.bean.Result;
import com.zjw.xm.entity.CrBook;
import com.zjw.xm.entity.CrCategory;
import com.zjw.xm.xmorder.mapper.CrBookMapper;
import com.zjw.xm.xmorder.service.impl.CrBookServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zjw1
 * @since 2022-08-03
 */
@RestController
@RequestMapping("/xm/crBook")
public class CrBookController {

    @Resource
    CrBookMapper crBookMapper;

    @Resource
    CrBookServiceImpl crBookService;

    @GetMapping("queryNew")
    //最新书本查询
    public List<CrBook> queryNew(){
        IPage<CrBook> page=new Page<>(1,12);
        //设置降序
        page.orders().add(OrderItem.desc("press_date"));
        page=crBookMapper.selectPage(page,null);
        return  page.getRecords();
    }

    @RequestMapping("findById")
    public CrBook findById(@RequestParam int id){
        return  crBookMapper.selectById(id);
    }






    @RequestMapping("/queryPageByPaking")
    public Page<CrBook> queryPageByPaking(@RequestParam String paking,
                                          @RequestParam int page,
                                          @RequestParam int size) {
        Page<CrBook> ipage = new Page<>(page, size);
        LambdaQueryWrapper<CrBook> lqw = new LambdaQueryWrapper();
        lqw.eq(CrBook::getPacking, paking);
        return crBookMapper.selectPage(ipage, lqw);
    }

    @RequestMapping("/queryPageByPakingDesc")
    public Page<CrBook> queryPageByPakingDesc(@RequestParam String paking,
                                          @RequestParam int page,
                                          @RequestParam int size) {
        Page<CrBook> ipage = new Page<>(page, size);
        ipage.orders().add(OrderItem.desc("press_date"));
        LambdaQueryWrapper<CrBook> lqw = new LambdaQueryWrapper();
        lqw.eq(CrBook::getPacking, paking);

        return crBookMapper.selectPage(ipage, lqw);
    }



    @RequestMapping("/queryPageByDxwx")
    public Page<CrBook> queryPageByDxwx(  @RequestParam int category2,
                                          @RequestParam int page,
                                          @RequestParam int size) {
        Page<CrBook> ipage = new Page<>(page, size);
        LambdaQueryWrapper<CrBook> lqw = new LambdaQueryWrapper();

        lqw.eq(CrBook::getCategory2, category2 );
        return crBookMapper.selectPage(ipage, lqw);
    }

    @RequestMapping("/queryPageByFznj")
    public Page<CrBook> queryPageByFznj(
            @RequestParam int page,
            @RequestParam int size) {
        Page<CrBook> ipage = new Page<>(page, size);

        QueryWrapper<CrBook> wrapper = Wrappers.query();
        wrapper.like("name","年").or().like("name","志");
        return crBookMapper.selectPage(ipage, wrapper);
    }

    @RequestMapping("/queryBookPage")
    public  Page<Map<String,Object>> queryBookPage(
            @RequestParam int id,
            @RequestParam int page,
            @RequestParam int size){
        Page<Map<String,Object>> iPage=new Page<>(page,size);
        iPage.setRecords(crBookMapper.selectBookPage(id,(page-1)*size,size));
        Map<String,Object> map = crBookMapper.countById(id);
        long total=Long.parseLong(map.get("cnt")+"");
        iPage.setTotal(total);
        return iPage;
    }

    @RequestMapping("/searchByName")
    public  Page<Map<String,Object>> searchByName(
            @RequestParam String name,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "15") int size){
        Page<Map<String,Object>> iPage=new Page<>(page,size);
        iPage.setRecords(crBookMapper.search(name,(page-1)*size,size));
        Map<String,Object> map = crBookMapper.countByName(name);
        long total=Long.parseLong(map.get("cnt")+"");
        iPage.setTotal(total);
        return iPage;
    }

    //后台书本的查询
    @RequestMapping("queryBackBooks")
    public Page<CrBook> queryBackBooks(@RequestParam(required = false) String name,
                                       @RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "7") int size){
        Page<CrBook> iPage=new Page<>(page,size);
        iPage.setRecords(crBookMapper.selectBackBooks(name,(page-1)*size,size));
        Map<String,Object> map = crBookMapper.countBackBooks(name);
        long total=Long.parseLong(map.get("cnt")+"");
        iPage.setTotal(total);
        return iPage;
    }

    //后台书本的删除
    @RequestMapping("del")
    public Result del(CrBook crBook){
        LambdaQueryWrapper<CrBook> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CrBook::getId,crBook.getId());
        CrBook book=crBookMapper.selectOne(lqw);
        crBookService.removeById(book);
        return  Result.success("图书删除成功",null);
    }

    //分类1的查询
    @RequestMapping("queryCate1")
    public List<Map<String,Object>> queryCate1(){
        return crBookMapper.queryCate1();
    }
    //分类2的查询
    @RequestMapping("queryCate2")
    public List<Map<String,Object>> queryCate2(Integer id){
        return crBookMapper.queryCate2(id);
    }

    //分类3的查询
    @RequestMapping("queryCate3")
    public List<Map<String,Object>> queryCate3(Integer id){
        return crBookMapper.queryCate3(id);
    }


    //后台书本的新增与编辑
    @RequestMapping("save")
    //@Transactional(rollbackFor = Exception.class)
    public Result save(CrBook crBook){
        LambdaQueryWrapper<CrBook> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CrBook::getId,crBook.getId());
        CrBook book=crBookMapper.selectOne(lqw);
        if(book==null){
            book = crBook;
            crBookMapper.insert(book);
            return Result.success("新增成功",null);
        }else {
            book = crBook;
            crBookService.saveOrUpdate(book);
            return Result.success("修改成功",null);
        }

    }

}

